<?php

class Relationship{

	private $registry;
	private $userid;
	private $profileid;
	private $friendsid;
	private $friendsprofileid;
	private $accepted;
	private $id = 0;
	private $type;
	private $result;
	
	public function getResult()
	{
		return $this->result;
	}

	/**
	 * Relationship constructor
	 * @param Registry $registry the registry
	 * @param int $id the relationship ID
	 * @param int $usera the id of user a
	 * @param int $userb the id of user b
	 * @param bool $approved if the relationship is approved
	 * @param int $type the ID of the relationship type
	 * @return void
	 */
	public function __construct( Registry $registry, $userid, $name, $photo,
	$friendsuserid, $profileid, $friendsprofileid, $type = 1, $id=0 )
	{
		$this->registry = $registry;
		// if no ID is passed, then we want to create a new relationship
		if( $id == 0 )
		{
			$this->createRelationship( $userid, $profileid, $name, $photo, $friendsuserid, $friendsprofileid, $type);
		}
		else
		{
			// if an ID is passed, populate based off that
			$sql = "SELECT * FROM relationships WHERE ID=" . $id;
			$this->registry->getObject('db')->executeQuery( $sql );
			if( $this->registry->getObject('db')->numRows() == 1 )
			{
				$data = $this->registry->getObject('db')->getRows();
				$this->populate( $data['ID'], $data['userid'], $data['profileid'], $data['friendid'], $data['friendprofileid'], $data['accepted'], $data['type'] );
			}
			else
				echo '<result>1</result>';

		}
	}

	/**
	 * Create a new relationship where one currently doesn't exist, if one does exist, populate from that
	 */
	public function createRelationship($userid, $profileid, $name, $photo, $friendsuid, $friendsprofileid, $type)
	{
		// check for pre-existing relationship
		$this->result = "Friend request sent";
		$sql = "SELECT * FROM relationships a WHERE ( (userid= {$userid} and profileid={$profileid}) AND (friendid={$friendsuid} AND friendprofileid={$friendsprofileid})) OR ((userid={$friendsuid} and profileid={$friendsprofileid}) AND (friendid={$userid} AND friendprofileid={$profileid}))";
		$this->registry->getObject('db')->executeQuery( $sql );
		if( $this->registry->getObject('db')->numRows() == 1 )
		{
			// one exists: populate
			$data = $this->registry->getObject('db')->getRows();
			$this->populate( $data['ID'], $data['userid'], $data['profileid'], $data['friendid'], $data['friendprofileid'], $data['accepted'] , $data['type']);
			
		}
		else
		{
			// one doesnt exist
			if( $type != 0 )
			{
				$this->accepted = 0;

				// check type for mutual
				$sql = "SELECT * FROM relationship_types WHERE ID=" . $type;
				$this->registry->getObject('db')->executeQuery( $sql );
				if( $this->registry->getObject('db')->numRows() == 1 )
				{
					$data = $this->registry->getObject('db')->getRows();
					// auto approve non-mutual relationships
					if( $data['mutual'] == 0 )
					{
						$this->accepted = 1;
					}
				}

				$this->userid = $userid;
				$this->profileid = $profileid;
				$this->friendsid = $friendsuid;
				$this->friendsprofileid = $friendsprofileid;
				$this->type = $type;

				// create the relationsip entry on the table
				$insert = array();
				$insert['userid'] = $this->userid;
				$insert['profileid'] = $this->profileid;
				$insert['friendid'] = $this->friendsid;
				$insert['friendprofileid'] = $this->friendsprofileid;
				$insert['type'] = $this->type;
				$insert['accepted'] = $this->accepted;

				$this->registry->getObject('db')->insertRecords( 'relationships', $insert );
				$this->id = $this->registry->getObject('db')->lastInsertID();

				// Add a notification to the other user
				$notification = array();
				$notification['type'] = 1;
				$notification['title'] = "$name would like to add you as a friend";
				$notification['ext_id'] = $this->id;
				$notification['user_id'] = $this->friendsid;
				$notification['profile_id'] = $this->friendsprofileid;
				$notification['time_stamp'] = date( 'Y-m-d H:i:s', time());
				$notification['has_read'] = 0;
				
				$this->registry->getObject('db')->insertRecords( 'notifications', $notification );

				// Send a mail to the friend
				$sql = "SELECT * FROM profile WHERE user_id= $friendsuid AND profile_id= $friendsprofileid";
				$this->registry->getObject('db')->executeQuery( $sql );
				
				if( $this->registry->getObject('db')->numRows() == 1 )
				{
					$data = $this->registry->getObject('db')->getRows();
					$friendsemail = $data['email'];

					$this->registry->getObject('mailout')->startFresh();
					$this->registry->getObject('mailout')->setTo($friendsemail);
					$this->registry->getObject('mailout')->setSender('');
					$this->registry->getObject('mailout')->setSubject( 'New Connection request at ' .$this->registry->getSetting('sitename') );
					$this->registry->getObject('mailout')->buildFromTemplates('emailtemplates/friendreq_eml.tpl');

					$tags['photo'] = $photo;
					$tags['name'] = $name;
					$tags['siteurl'] = $this->registry->getSetting('siteurl');
						
					$this->registry->getObject('mailout')->replaceTags( $tags );
					$this->registry->getObject('mailout')->setMethod('sendmail');
					$this->registry->getObject('mailout')->appendHeader('MIME-Version: 1.0');
					$this->registry->getObject('mailout')->appendHeader('Content-Type: text/html; charset=ISO-8859-1');
					$this->registry->getObject('mailout')->send();
				}
			}
		}
	}

	/**
	 * Approve relationship
	 * @return void
	 */
	public function approveRelationship()
	{
		$this->accepted = true;
	}


	/**
	 * Delete relationship
	 * @return void
	 */
	public function delete()
	{
		$this->registry->getObject('db')->deleteRecords( 'relationships', 'ID=' . $this->id, 1 );
		$this->id = 0;
	}

	/**
	 * Save relationship
	 * @return void
	 */
	public function save()
	{
		$changes = array();
		$changes['userid'] = $this->userid;
		$changes['profileid'] = $this->profileid;
		$changes['friendid'] = $this->friendsid;
		$changes['friendprofileid'] = $this->friendsprofileid;
		$changes['type'] = $this->type;
		$changes['accepted'] = $this->accepted;
		$this->registry->getObject('db')->updateRecords( 'relationships', $changes, "ID=" . $this->id );
	}

	/**
	 * Populate relationship object
	 * @param int $id the user id
	 * @param int $usera user a
	 * @param int $userb user b
	 * @param int $type the type
	 * @param bool $approved
	 * @return void
	 */
	private function populate( $id, $userid, $profileid, $friendid, $friendprofileid, $approved, $type )
	{
		$this->id = $id;
		$this->type = $type;
		$this->userid = $userid;
		$this->profileid = $profileid;
		$this->friendsid = $friendid;
		$this->friendsprofileid = $friendprofileid;
		$this->accepted = $approved;
		$this->type = $type;
	}

	public function isApproved()
	{
		return $this->accepted;
	}

	public function getFriendsID()
	{
		return $this->friendsid;
	}

	public function getFriendsPID()
	{
		return $this->friendsprofileid;
	}

	public function getUserID()
	{
		return $this->userid;
	}

	public function getProfileID()
	{
		return $this->profileid;
	}
}

?>